<template>
  <div class="login-form-wrapper">
    <el-form
      :model="form"
      :rules="rules"
      ref="loginForm"
      class="glass-form"
      label-position="top"
    >
      <h2 class="form-title">登录</h2>

      <div class="form-grid">
        <el-form-item label="用户名:" prop="username" >
          <el-input v-model="form.username" size="large"></el-input>
        </el-form-item>

        <el-form-item label="密__码:" prop="password" >
          <el-input
            type="password"
            v-model="form.password"
            show-password
            size="large"
          ></el-input>
        </el-form-item>

        <el-button
          type="primary"
          :loading="loading"
          :disabled="loading"
          @click="submitForm"
          class="submit-btn"
        >
          {{ loading ? '验证中' : '登录' }}
        </el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import {ElForm, ElFormItem, ElInput, ElButton, ElMessage} from 'element-plus'
import axios from "axios"
import setting from "@/setting.js"
import router from "@/router/index.js"
import { jsonApi } from "@/global.js"


// 移除了defineEmits的导入，直接使用编译器宏

const form = ref({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}
const loginForm = ref(null)
const loading = ref(false)

const handleLogin = async (credentials) => {
  try {
    loading.value = true
    const response = await jsonApi('/user/login/', JSON.stringify(credentials), false)
    const data = await response.json()

    if (data.message === 'Login successful!') {
      localStorage.setItem('access_token', data.access_token)
      localStorage.setItem('refresh_token', data.refresh_token)
      localStorage.setItem('user_groups', data.user_groups)

      loading.value = false
      await router.push('/main')
    } else {
      loading.value = false
      ElMessage.error('登录失败: 用户名或密码有误')
    }
  } catch (error) {
    console.error(error)
    ElMessage.error(`登录失败，原因是: ${error}`)
    loading.value = false
  }
}

const submitForm = () => {
  loginForm.value.validate(valid => {
    if (valid) {
      handleLogin(form.value)
    } else {
      ElMessage.error('表单验证失败!')
      return false
    }
  })
}
</script>

<style scoped>
.login-form-wrapper {
  position: relative;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
}

.glass-form {
  width: 100%;
  max-width: 420px;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 16px;
  padding: 30px 25px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.15);
  box-sizing: border-box;
}

.form-title {
  text-align: center;
  margin-bottom: 20px;
  color: #ffffffdd;
  font-size: 1.8rem;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 18px;
}

.form-item :deep(.el-form-item__label) {
  color: #ffffffcc;
  font-weight: 500;
  margin-bottom: 6px;
}

.form-item :deep(.el-input__inner) {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #fff;
  border-radius: 8px;
  height: 40px;
  padding-left: 12px;
  font-size: 0.95rem;
}

.form-item :deep(.el-input__inner::placeholder) {
  color: #ffffffaa;
}

.submit-btn {
  width: 100%;
  height: 42px;
  font-size: 1rem;
  font-weight: 600;
  background: linear-gradient(45deg, #4f46e5, #3b82f6);
  border: none;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(59, 130, 246, 0.4);
  background: linear-gradient(45deg, #3b82f6, #4f46e5);
}

/* 响应式设计 */
@media (min-width: 768px) {
  .glass-form {
    padding: 35px 30px;
    max-width: 450px;
  }
}

@media (max-width: 450px) {
  .glass-form {
    border-radius: 12px;
    padding: 22px 18px;
  }

  .form-title {
    font-size: 1.6rem;
  }

  .form-grid {
    gap: 15px;
  }
}
</style>